import {ActionTree, createStore, GetterTree, ModuleTree, MutationTree, Plugin} from "vuex"

import * as type from './types'
import {LanguageType} from "../api/LanguageRequest";
import {State} from "./types";
import {clearCookie, cookiesType, setCookie} from "../utils/cookies";


const store = createStore<State>({
    state: {
        config: {
            token: '',
            refreshToken: '',
            languageId: '',
        },
        globalInfo: {
            languageMap: new Map<string, LanguageType>(),
            userInfo: null,
            cartInfo: null,
            mainPageInfo: null,
            companyInfo: null,
            goodTypeTreeList:[]
        },
        visible: {
            actionBar: false,
            shopCart: false
        }
    },
    getters: {},
    actions: {},
    mutations: {
        'SET_ACTIONBAR': (state, payload: boolean) => {
            state.visible.actionBar = payload;
        },
        'SET_SHOPCART': (state, payload: boolean) => {
            state.visible.shopCart = payload;
        },
        'SET_GLOBAL_INFO': (state, payload: type.GlobalInfoType) => {
            if (payload.languageList != null && payload.languageList.length > 0) {
                let map = new Map<string, LanguageType>();
                let enId: string = '';
                payload.languageList.forEach((value, index, array) => {
                    if (typeof value.id === "string") {
                        if (value.language?.toLowerCase().trim().startsWith('en')) {
                            enId = value.id;
                        }
                        map.set(value.id, value);
                    }
                })
                state.globalInfo.languageMap = map
                if (enId != null && enId.trim().length > 0) {
                    if (state.config.languageId == null || state.config.languageId.trim().length == 0 || !map.has(state.config.languageId)) {
                        state.config.languageId = enId
                    }
                }
            }
            if (payload.languageMap != null) {
                state.globalInfo.languageMap = payload.languageMap
            }
            if (payload.cartInfo != null) {
                state.globalInfo.cartInfo = payload.cartInfo
            }
            if (payload.userInfo != null) {
                state.globalInfo.userInfo = payload.userInfo
            }
            if (payload.mainPageInfo != null) {
                state.globalInfo.mainPageInfo = payload.mainPageInfo
            }
            if (payload.companyInfo != null) {
                state.globalInfo.companyInfo = payload.companyInfo
            }
            if (payload.goodTypeTreeList != null) {
                state.globalInfo.goodTypeTreeList = payload.goodTypeTreeList
            }
        },
        'SET_CONFIG': (state, payload: type.GlobalConfigType) => {
            if (payload.token != undefined && payload.token.toString().trim().length > 0) {
                state.config.token = payload.token.toString().trim()
                setCookie(cookiesType.token, state.config.token)
            }
            if (payload.refreshToken != undefined && payload.refreshToken.toString().trim().length > 0) {
                state.config.refreshToken = payload.refreshToken.toString().trim()
                setCookie(cookiesType.refreshToken, state.config.refreshToken)
            }
            if (payload.languageId != undefined && payload.languageId.toString().trim().length > 0) {
                state.config.languageId = payload.languageId.toString().trim()
                setCookie(cookiesType.languageId, state.config.languageId)
            }
        },
        "CLEAR_LOGIN_INFO": (state) => {
            state.config.token=null
            state.config.refreshToken=null
            state.globalInfo.userInfo=null
            clearCookie();
        }
    },
    modules: {}
})

export default store;
